/*
 * @(#)UPConnectorImpl.java        1.0 2009-9-9
 *
 * Copyright (c) 2007-2009 Shanghai Handpay IT, Co., Ltd.
 * 16/F, 889 YanAn Road. W., Shanghai, China
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of 
 * Shanghai Handpay IT Co., Ltd. ("Confidential Information").  
 * You shall not disclose such Confidential Information and shall use 
 * it only in accordance with the terms of the license agreement you 
 * entered into with Handpay.
 */

package com.handpay.framework.utils;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;

import com.handpay.zztong.hp.log.HPLog;

public class UPConnect{
	public static final byte CMNET_TYPE=1;	
	public static final byte CMWAP_TYPE=2;
	public static byte NET_type=CMNET_TYPE;
	protected byte currentConnectType=NET_type;
	private String content = null;
	private String url=null;
	private int cacheBufferSize = 1000;
	public UPConnect(String content,String url) {
		this.content = content+" ";
		this.url=url;
	}
	
	public String execute(){
		return this.connectDo();
	}

	/**
	 * @see com.handpay.client.api.net.AbstractConnectorImpl#execute()
	 */
	private String connectDo() {

		DataInputStream dis = null;
		ByteArrayOutputStream baos = null;
		try {
			
			
			BasicHttpParams httpParameters = new BasicHttpParams();
			HttpConnectionParams.setConnectionTimeout(httpParameters, 60000);
			HttpConnectionParams.setSoTimeout(httpParameters, 60000); 
			HttpClient client = new DefaultHttpClient(httpParameters);

//			String url = ClientEngine.getRealHttpAddr(this.netdata.addrname)
//					+ this.netdata.action;
//			String url = "http://202.96.255.146:8080/Gateway/MobilePayment";//生产
//			String url = "http://202.101.25.178:8080/Gateway/MobilePayment";//测试
			HPLog.e("加载的url", url);
			HttpPost request = new HttpPost(url);



			ByteArrayEntity postcontent = new ByteArrayEntity(content.getBytes());
			request.setEntity(postcontent);
			HttpResponse response = client.execute(request);
			int responseCode = response.getStatusLine().getStatusCode();
			// String responseContentType = httpConn.getType();

			if (responseCode != HttpStatus.SC_OK
					&& responseCode != HttpStatus.SC_PARTIAL_CONTENT) {
				//this.netdata.errCode = responseCode;
				//this.netdata.errMsg = Errors.ERR_MSG_HTTP;
				return "error";
			} else {
				//this.netdata.errCode = 0;
			}

			dis = new DataInputStream(response.getEntity().getContent());

			baos = new ByteArrayOutputStream();

			int len = -1;

			byte[] cache = new byte[this.cacheBufferSize];
			int curSize = 0;
			while (true) {
				try {
					len = dis.read(cache);
					if (len < 0
							|| (len == 2 && cache[0] == 0x0D && cache[1] == 0x0A)) {
						break;
					}
				} catch (IOException ie) {
					break;
				}

				baos.write(cache, 0, len);

				curSize += len;
				cache = new byte[this.cacheBufferSize];
			}

			byte[] responseData = baos.toByteArray();
			if (responseData != null) {
				return new String(responseData, "UTF-8");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				baos.close();
			} catch (Exception e) {
			}
			try {
				dis.close();
			} catch (Exception e) {
			}
		}
		return "error";
	}

}
